<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>维护单元列表</title>

    <script type="text/javascript">
      var editMaintenanceWin;
      var checkInWin;
      var checkOutWin;
      var mId;

      Ext.onReady(function(){

        var trainId = <%=params[:trainId]%>
        var taskBeginMinute = <%=params[:taskBeginMinute]%>
        var trainTaskId = <%=params[:trainTaskId]%>
        var isEnd = <%=params[:isEnd]%>
        var sub_type = <%=params[:sub_type]%>

        maintenanceDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit",
            trainTaskId: "trainTaskId"
          },
          baseParams: {
            start: 0,
            limit: pageSize,
            trainTaskId: trainTaskId
          },
          proxy: new Ext.data.HttpProxy({
            url: '/maintenance/getMaintenances'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'total',
            root: 'root'
          }, [
            {name: 'id'},
            {name: 'train_task_id'},
            {
              name: 'work_name',
              mapping: 'work',
              convert: function(v){
                if(v){
                  return v.name
                }else{
                  return '员工已删除'
                }
              }
            },
            {
              name: 'team_name',
              mapping: 'team',
              convert: function(v){
                if(v){
                  return v.name
                }else{
                  return '已删除'
                }
              }
            },
            {
              name: 'worker_name',
              mapping: 'worker',
              convert:function(v){
                if(v){
                  return v.name
                }else{
                  return ""
                }
              }
            },
            {name: 'description'},
            {name: 'begin_time'},
            {name: 'finish_time'},
            {name: 'dynamic_flag'},
            {
              name: 'dflag',
              mapping: 'dynamic_flag',
              convert:function(v){
                if(v==0){
                  return '<font color=gray>无限制</font>'
                }else if(v==1){
                  return '<font color=green>有静有动</font>'
                }else{
                  return '<font color=red>有静无动</font>'
                }
              }
            },
            {
              name:'plan_time',
              convert:function(v,record){
                return Math.floor((new Date(record.finish_time)-new Date(record.begin_time))/60000)
              }
            },
            {name:'check_in_time'},
            {name:'check_out_time'},
            {
              name:'flag',
              mapping:'flag',
              convert: function(v,r){
                if(v == '1'){
                  return '等待断电'
                }else if(v == '2'){
                  return '静态作业中'
                }else if(v == '3'){
                  return '静态作业正常结束'
                }else if(v == '4'){
                  return '静态作业正常结束'
                }else if(v == '5'){
                  return '静态作业手动结束'
                }else if(v == '6'){
                  if(r.dynamic_flag==1){
                    return '静态作业正常结束'
                  }else{
                    return '动静态作业正常结束'
                  }
                }else if(v == '7'){
                  if(r.dynamic_flag==1){
                    return '静态作业正常结束，动态作业手动结束'
                  }else{
                    return '静态作业正常结束'
                  }
                }else if(v == '8'){
                  return '静态作业手动结束，动态作业正常结束'
                }else if(v == '9'){
                  if(r.dynamic_flag==1){
                    return '静态作业手动结束，动态作业手动结束'
                  }else{
                    return '静态作业手动结束'
                  }
                  
                }
              }
            },
            {
              name:'work_time',
              convert:function(v,record){
                if(record.check_out_time==null){
                  if(record.check_in_time==null){
                    return 0
                  }else{
                    return Math.floor((new Date()-new Date(record.check_in_time))/60000)
                  }                  
                }else{
                  if(record.check_in_time==null){
                    return 0
                  }else{
                    return Math.floor((new Date(record.check_out_time)-new Date(record.check_in_time))/60000)
                  }
                  
                }
                
              }
            }
          ])
        });

        var maintenanceSM = new Ext.grid.CheckboxSelectionModel();
        var maintenanceCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer({width: 30}),maintenanceSM,
          {header: '<center>作业类型</center>',dataIndex: 'dflag',width:35},
          {header: '计划开工时间',width:80,sortable:true,dataIndex:'begin_time',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')},
          {header: '<center>计划<br/>完工时间</center>',width:55,sortable:true,dataIndex:'finish_time',renderer: Ext.util.Format.dateRenderer('m-d H:i')},
          {header: '计划<br>时长',width:35,sortable:true,dataIndex:'plan_time'},
          {header: '组队',dataIndex: 'team_name',width:125},
          {header: '打卡<br>人员',dataIndex: 'description',width:65},
          {header: '开工时间',width:50,sortable:true,dataIndex:'check_in_time',renderer: Ext.util.Format.dateRenderer('m-d H:i')},
          {header: '完工时间',width:50,sortable:true,dataIndex:'check_out_time',renderer: Ext.util.Format.dateRenderer('m-d H:i')},
          {header: '实际<br>时长',width:35,sortable:true,dataIndex:'work_time'},
          {header: '状态',width:60,sortable:true,dataIndex:'flag'}
        ]);

        var maintenanceGrid =  new Ext.grid.GridPanel({
          ds: maintenanceDS,
          cm: maintenanceCM,
          sm: maintenanceSM,
          width: 1200,
          height: 535,
          iconCls: 'icon-grid',
          viewConfig:{
            forceFit:true
          },
          tbar: ['-',{
              text: '新增维护单元',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newMaintenanceWin = new Ext.Window({
                  layout: 'fit',
                  autoScroll: true,
                  width:800,
                  height:605,
                  draggable: true ,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  title: '新增维护单元',
                  autoLoad: {
                    url: "/maintenance/newMaintenance",
                    params: {
                      trainTaskId: trainTaskId
                    },
                    scripts: true
                  }
                });
                newMaintenanceWin.on("destroy",function(){
                  maintenanceDS.reload();
                });
                newMaintenanceWin.resizable = false;
                newMaintenanceWin.show();
              }
            },
            {
              text: '编辑维护单元',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = maintenanceGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  var maintenanceId = records[0].get('id');
                  editMaintenanceWin = new Ext.Window({
                    title: '编辑维护单元',
                    layout:'fit',
                    width:800,
                    height:605,
                    autoScroll: true,
                    draggable: true ,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/maintenance/editMaintenance",
                      params: {
                        maintenanceId: maintenanceId
                      },
                      scripts: true
                    }
                  });
                  editMaintenanceWin.on("destroy",function(){
                    maintenanceDS.reload();
                  });
                  editMaintenanceWin.resizable = false;
                  editMaintenanceWin.show();
                }
              }
            },
            {text:'删除',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(maintenanceGrid.getSelectionModel().hasSelection()){
                  var rs=maintenanceGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var workName = '';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    workName+=data.work_name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下维护单元？<br><font color="red">' + workName + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/maintenance/delete',
                        method:'post',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            maintenanceDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除维护单元。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的维护单元');
                }
              }
            },'-',{
              text:'进度图预览',
              width:50,
              iconCls:'chart',
              handler:function(){
                readChartWin = new Ext.Window({                                         //window和上面的fuction不能重名，否则会出问题
                  layout: 'fit',
                  width:939,
                  height:642,
                  closeAction: 'destroy',
                  autoLoad: {
                    url: "/maintenance/chart",
                    params:{
                      trainTaskId:trainTaskId,
                      taskBeginMinute:taskBeginMinute,
                      sub_type:sub_type,
                      isEnd:isEnd,
                      trainId:trainId
                    },
                    scripts: true
                  },
                  plain: true,
                  modal: true,
                  constrain:true,
                  autoScroll: true,
                  draggable: false                                                      //不能拖动
                });
                readChartWin.on("destroy",function(){
                  clearInterval(interval);
                  trainTaskDS.reload();
                });
                readChartWin.setTitle(trainTaskSn+"的工作进度表");
                readChartWin.resizable = false;
                readChartWin.show();
              }
            },'-',
            {text:'结束维护任务',
              width:50,
              iconCls: 'remove',
              handler:function(){
                Ext.MessageBox.confirm('警告','确定结束当前维护任务？<br>（结束后将无法编辑维护单元）',function (button){
                  if(button=='yes'){
                    Ext.Ajax.request({
                      url:'/traintask/finishTrainTask',
                      params: {"trainTaskId":trainTaskId},
                      method:'post',
                      success:function(response,opts){
                        var data = Ext.util.JSON.decode(response.responseText);
                        if(data.success&&data.info=='success') {
                          maintenanceDS.reload();
                          Ext.Msg.alert('提示信息', '已成功结束维护任务。');
                          readMaintenanceWin.destroy();
                        } else {
                          Ext.Msg.alert('提示信息', data.info);
                        }
                      }
                    });
                  }
                });
              }
            }
          ],
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,                                                 //显示条目的多少
            store: maintenanceDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });

        function changeType(type){
          Ext.Ajax.request({
            url:'/maintenance/assign?type='+type,
            method:'post',
            waitMsg: '正在修改养护任务作业类型...',
            success:function(response,opts){
              var data = Ext.util.JSON.decode(response.responseText);
              if(data.success&&data.info=='success') {
                maintenanceDS.reload();
              } else {
                Ext.Msg.alert('提示信息', data.info);
              }
            },
            params: {"id": mId}
          });
        }

        maintenanceGrid.on('rowcontextmenu', function(grid,rowindex,e){
          mId = maintenanceDS.getAt(rowindex).get('id')
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        var rMenu1 = new Ext.menu.Item({
          text:'无限制',
          handler: function(){
            changeType(0)
          }
        })

        var rMenu2 = new Ext.menu.Item({
          text:'有静有动',
          handler: function(){
            changeType(1)
          }
        })

        var rMenu3 = new Ext.menu.Item({
          text:'有静无动',
          handler: function(){
            changeType(2)
          }
        })
        var rightClick = new Ext.menu.Menu({
          id:'rightClickCont',
          items: [
            rMenu1,rMenu2,rMenu3
          ]
        });
        //如果任务已经结束，则不能进行编辑
        if(isEnd=='1'){
          maintenanceGrid.getTopToolbar().disabled = true;
          rightClick.disabled = true;
        }
        maintenanceGrid.render('maintenanceList');
        maintenanceDS.load();                                                   //先进行render，在进行DS.load()，其实顺序无所谓
      })
    </script>
  </head>
  <body>
    <div id="maintenanceList"></div>
  </body>
</html>